package com.infonautics.dao.imp;

import java.util.List;

import com.hibernate.Company;
import com.hibernate.HibernateDao;
import com.infonautics.dao.ICompanyDao;
import com.infonautics.page.PageBean;
import com.system.dao.ISystemDao;

public class ICompanyDaoImp implements ISystemDao<Company>,ICompanyDao{

	HibernateDao<Company> companyDao = new HibernateDao<Company>();
	
	@Override
	public PageBean<Company> doList(int pageSize, int page,
			int id) throws Exception {
		return null;
	}

	@Override
	public void doAdd(Company object) throws Exception {
		if(object != null){
			companyDao.addObject(object);
		}
	}

	@Override
	public void doModfiy(Company object) throws Exception {
		if(object != null){
			companyDao.updateObject(object);
		}
	}

	@Override
	public void doDelete(int id) throws Exception {
		companyDao.deleteObject(id, Company.class);
		
	}

	@Override
	public Company doOne(int id) throws Exception {
		return companyDao.getObject(id, Company.class);
	}

	@Override
	public PageBean<Company> doCompanyList(int pageSize, int page)
			throws Exception {
		final String hql = "from Company order by id desc";
		int allRow = companyDao.getAllRowCount("select count(*) from Company");
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);
		List<Company> list = companyDao.getObjects(hql, offset,length);
	
		PageBean<Company> pageBean = new PageBean<Company>();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

	@Override
	public PageBean<Company> doQueryList(int pageSize, int page, String sql)
			throws Exception {
		final String hql = "from Company  where 1=1 "+sql+" order by id desc";
		int allRow = companyDao.getAllRowCount("select count(*) from Company where 1=1 "+sql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int length = pageSize;
		final int currentPage = PageBean.countCurrentPage(page);
		List<Company> list = companyDao.getObjects(hql, offset,length);
	
		PageBean<Company> pageBean = new PageBean<Company>();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		return pageBean;
	}

	@Override
	public List<Company> doCompanyAll() throws Exception {
		String hql = "from Company order by id";
		return companyDao.getListObjects(hql);
	}

}
